package com.tdtxdcxm.telmanagesys.slice;


import com.tdtxdcxm.telmanagesys.PeopleInfoConfig;
import com.tdtxdcxm.telmanagesys.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.window.dialog.ToastDialog;


public class PersonDetailSlice extends AbilitySlice {
    private Button persondetail_backbut,persondetail_nobut,persondetail_okeditbut;

    private Text persondetail_nametext;
    private TextField persondetail_teltfd,persondetail_teltfd2;
    private TextField persondetail_emailtfd,persondetail_emailtfd2;
    private TextField persondetail_worktfd,persondetail_worktfd2;
    private TextField persondetail_addresstfd,persondetail_addresstfd2;

    private boolean isssave = false;

    public void toastShow(String s){
        ToastDialog toastDialog = new ToastDialog(this.getContext());
        toastDialog.setText(s);
        toastDialog.setTransparent(true);
        toastDialog.setDuration(1000);
        toastDialog.setAlignment(LayoutAlignment.BOTTOM);
        toastDialog.show();
    }
    public void commonDialogShow(String titletext,String contenttext){
        CommonDialog commonDialog = new CommonDialog(this.getContext());
        commonDialog.setTitleText(titletext);
        commonDialog.setContentText(contenttext);
        commonDialog.setAlignment(LayoutAlignment.CENTER);
        commonDialog.setCornerRadius(20);
        commonDialog.setSize(700,360);
        commonDialog.setButton(1, "确定", new IDialog.ClickedListener() {
            @Override
            public void onClick(IDialog iDialog, int i) {
                commonDialog.destroy();
                terminate();
            }
        });
        commonDialog.setButton(2, "取消", new IDialog.ClickedListener() {
            @Override
            public void onClick(IDialog iDialog, int i) {
                commonDialog.destroy();
            }
        });
        commonDialog.show();
    }

    public void initPDSilceComponents(String name){
        persondetail_backbut = (Button) findComponentById(ResourceTable.Id_persondetail_backbut);
        persondetail_nobut= (Button) findComponentById(ResourceTable.Id_persondetail_nobut);
        persondetail_okeditbut = (Button) findComponentById(ResourceTable.Id_persondetail_okeditbut);

        persondetail_nametext = (Text) findComponentById(ResourceTable.Id_persondetail_nametext);
        //persondetail_nametext.setText(name);

        persondetail_teltfd = (TextField) findComponentById(ResourceTable.Id_persondetail_teltfd);
        persondetail_teltfd2 = (TextField) findComponentById(ResourceTable.Id_persondetail_teltfd2);
        persondetail_emailtfd = (TextField) findComponentById(ResourceTable.Id_persondetail_emailtfd);
        persondetail_emailtfd2 = (TextField) findComponentById(ResourceTable.Id_persondetail_emailtfd2);
        persondetail_worktfd = (TextField) findComponentById(ResourceTable.Id_persondetail_worktfd);
        persondetail_worktfd2 = (TextField) findComponentById(ResourceTable.Id_persondetail_worktfd2);
        persondetail_addresstfd = (TextField) findComponentById(ResourceTable.Id_persondetail_addresstfd);
        persondetail_addresstfd2 = (TextField) findComponentById(ResourceTable.Id_persondetail_addresstfd2);

        String[] personinfo = PeopleInfoConfig.readPersonData(this.getContext(),name);
        persondetail_nametext.setText(personinfo[0]);
        persondetail_teltfd.setText(personinfo[1]);
        persondetail_teltfd2.setText(personinfo[2]);
        persondetail_emailtfd.setText(personinfo[3]);
        persondetail_emailtfd2.setText(personinfo[4]);
        persondetail_worktfd.setText(personinfo[5]);
        persondetail_worktfd2.setText(personinfo[6]);
        persondetail_addresstfd.setText(personinfo[7]);
        persondetail_addresstfd2.setText(personinfo[8]);

        persondetail_teltfd.setClickable(false);
        persondetail_teltfd2.setClickable(false);
        persondetail_emailtfd.setClickable(false);
        persondetail_emailtfd2.setClickable(false);
        persondetail_worktfd.setClickable(false);
        persondetail_worktfd2.setClickable(false);
        persondetail_addresstfd.setClickable(false);
        persondetail_addresstfd2.setClickable(false);

        persondetail_backbut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(isssave == false){
                    commonDialogShow("返回","还未保存，确定退出吗？");
                }
                else{
                    commonDialogShow("返回","已经保存，确定退出吗？");
                }
            }
        });
        persondetail_nobut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                persondetail_okeditbut.setText("编辑");

                persondetail_teltfd.clearFocus();
                persondetail_teltfd2.clearFocus();
                persondetail_emailtfd.clearFocus();
                persondetail_emailtfd2.clearFocus();
                persondetail_worktfd.clearFocus();
                persondetail_worktfd2.clearFocus();
                persondetail_addresstfd.clearFocus();
                persondetail_addresstfd2.clearFocus();

                persondetail_teltfd.setClickable(false);
                persondetail_teltfd2.setClickable(false);
                persondetail_emailtfd.setClickable(false);
                persondetail_emailtfd2.setClickable(false);
                persondetail_worktfd.setClickable(false);
                persondetail_worktfd2.setClickable(false);
                persondetail_addresstfd.setClickable(false);
                persondetail_addresstfd2.setClickable(false);

                toastShow("已退出编辑状态！");
            }
        });
        persondetail_okeditbut.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(persondetail_okeditbut.getText().equals("编辑") && personinfo[0].equals("") == false){
                    persondetail_teltfd.setClickable(true);
                    persondetail_teltfd2.setClickable(true);
                    persondetail_emailtfd.setClickable(true);
                    persondetail_emailtfd2.setClickable(true);
                    persondetail_worktfd.setClickable(true);
                    persondetail_worktfd2.setClickable(true);
                    persondetail_addresstfd.setClickable(true);
                    persondetail_addresstfd2.setClickable(true);
                    persondetail_okeditbut.setText("保存");
                    isssave = false;
                    toastShow("可以修改编辑！");
                    return;
                }
                if(persondetail_okeditbut.getText().equals("保存")){
                    String[] tpersoninfo = new String[9];
                    tpersoninfo[0] = persondetail_nametext.getText();
                    tpersoninfo[1] = persondetail_teltfd.getText();
                    tpersoninfo[2] = persondetail_teltfd2.getText();
                    tpersoninfo[3] = persondetail_emailtfd.getText();
                    tpersoninfo[4] = persondetail_emailtfd2.getText();
                    tpersoninfo[5] = persondetail_worktfd.getText();
                    tpersoninfo[6] = persondetail_worktfd2.getText();
                    tpersoninfo[7] = persondetail_addresstfd.getText();
                    tpersoninfo[8] = persondetail_addresstfd2.getText();

                    PeopleInfoConfig.writePersonData(PersonDetailSlice.this.getContext(),tpersoninfo);
                    persondetail_okeditbut.setText("编辑");
                    persondetail_nobut.getClickedListener().onClick(persondetail_nobut);
                    isssave = true;
                    toastShow("已经保存！");
                }
            }
        });
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_persondetail);
        initPDSilceComponents(intent.getStringParam("姓名"));
        System.out.println("开始了-----》》》》》》");

    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }
}
